#######################################################################################
### R file to replicate the figures for Laron K. Williams, "Guns Yield Butter?  An Exploration of Defense Spending Preferences", Journal of Conflict Resolution
###
### Created: 1-14-15
### Modified: 1-29-18 (JCR R&R)
###
#######################################################################################

library(foreign)
library(ggplot2)
library(lattice)
library(fields)
#library(separationplot)
library(scales)
library(gridExtra)

### Set up working directory
#setwd("")


#######################################################################################
### Figure 1: Relationship between health spending and defense spending, conditional on preferences for government-financed job creation
#######################################################################################
b <- read.dta("Substantive Effects.dta", convert.underscore = TRUE) 

b1.0 <- subset(b, model == "All" & create == 0)
b1.1 <- subset(b, model == "All" & create == 1)
b2.0 <- subset(b, model == "US Only" & create == 0)
b2.1 <- subset(b, model == "US Only" & create == 1)
b3.0 <- subset(b, model == "No US" & create == 0)
b3.1 <- subset(b, model == "No US" & create == 1)

### All Model, Create Jobs = 0
f11 <- ggplot(b1.0, aes(outcome, diff))
f11 <- f11 + geom_linerange(aes(ymin = diff.lo, ymax = diff.hi)) + geom_pointrange(aes(ymin = diff.lo, ymax = diff.hi))
f11 <- f11 + geom_hline(yintercept = 0, linetype = "dashed") + ggtitle("Create Jobs = 0") + xlab("") + ylab("    All    ") 
f11 <- f11 + scale_x_continuous(breaks = seq(1,5,1), labels = c("", "", "", "", ""))
f11 <- f11 + expand_limits(y=c(-0.125,0.125), x=5.25)
f11 <- f11 + theme_bw()
f11 <- f11 + theme(axis.title.y = element_text(angle = 0, size = 16))

### All Model, Create Jobs = 1
f12 <- ggplot(b1.1, aes(outcome, diff))
f12 <- f12 + geom_linerange(aes(ymin = diff.lo, ymax = diff.hi)) + geom_pointrange(aes(ymin = diff.lo, ymax = diff.hi))
f12 <- f12 + geom_hline(yintercept = 0, linetype = "dashed") + ggtitle("Create Jobs = 1") + xlab("") + ylab("") 
f12 <- f12 + scale_x_continuous(breaks = seq(1,5,1), labels = c("", "", "", "", ""))
f12 <- f12 + expand_limits(y=c(-0.125,0.125), x=5.25)
f12 <- f12 + theme_bw()
f12 <- f12 + theme(axis.title.y = element_text(angle = 0, size = 16))

### US Only Model, Create Jobs = 0
f21 <- ggplot(b2.0, aes(outcome, diff))
f21 <- f21 + geom_linerange(aes(ymin = diff.lo, ymax = diff.hi)) + geom_pointrange(aes(ymin = diff.lo, ymax = diff.hi))
f21 <- f21 + geom_hline(yintercept = 0, linetype = "dashed") + xlab("") + ylab("US Only") 
f21 <- f21 + scale_x_continuous(breaks = seq(1,5,1), labels = c("", "", "", "", ""))
f21 <- f21 + expand_limits(y=c(-0.125,0.125), x=5.25)
f21 <- f21 + theme_bw()
f21 <- f21 + theme(axis.title.y = element_text(angle = 0, size = 16))

### All Model, Create Jobs = 1
f22 <- ggplot(b2.1, aes(outcome, diff))
f22 <- f22 + geom_linerange(aes(ymin = diff.lo, ymax = diff.hi)) + geom_pointrange(aes(ymin = diff.lo, ymax = diff.hi))
f22 <- f22 + geom_hline(yintercept = 0, linetype = "dashed") + xlab("") + ylab("") 
f22 <- f22 + scale_x_continuous(breaks = seq(1,5,1), labels = c("", "", "", "", ""))
f22 <- f22 + expand_limits(y=c(-0.125,0.125), x=5.25)
f22 <- f22 + theme_bw()
f22 <- f22 + theme(axis.title.y = element_text(angle = 0, size = 16))

### No US Model, Create Jobs = 0
f31 <- ggplot(b3.0, aes(outcome, diff))
f31 <- f31 + geom_linerange(aes(ymin = diff.lo, ymax = diff.hi)) + geom_pointrange(aes(ymin = diff.lo, ymax = diff.hi))
f31 <- f31 + geom_hline(yintercept = 0, linetype = "dashed") + xlab("") + ylab("  No US ") 
f31 <- f31 + scale_x_continuous(breaks = seq(1,5,1), labels = c("Much Less", "Less", "Same", "More", "Much More"))
f31 <- f31 + expand_limits(y=c(-0.125,0.125), x=5.25)
f31 <- f31 + theme_bw()
f31 <- f31 + theme(axis.title.y = element_text(angle = 0, size = 16), axis.text.x = element_text(size = 8))

### No US Model, Create Jobs = 1
f32 <- ggplot(b3.1, aes(outcome, diff))
f32 <- f32 + geom_linerange(aes(ymin = diff.lo, ymax = diff.hi)) + geom_pointrange(aes(ymin = diff.lo, ymax = diff.hi))
f32 <- f32 + geom_hline(yintercept = 0, linetype = "dashed") + xlab("") + ylab("") 
f32 <- f32 + scale_x_continuous(breaks = seq(1,5,1), labels = c("Much Less", "Less", "Same", "More", "Much More"))
f32 <- f32 + expand_limits(y=c(-0.125,0.125), x=5.25)
f32 <- f32 + theme_bw()
f32 <- f32 + theme(axis.title.y = element_text(angle = 0, size = 16), axis.text.x = element_text(size = 8))

### Combine all the figures into one
grid.newpage()
pushViewport(viewport(layout = grid.layout(3,7)))

vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
print(f11, vp = vplayout(1, 1:4))
print(f12, vp = vplayout(1, 5:7))
print(f21, vp = vplayout(2, 1:4))
print(f22, vp = vplayout(2, 5:7))
print(f31, vp = vplayout(3, 1:4))
print(f32, vp = vplayout(3, 5:7))


#######################################################################################
### Figure 2: survey-specific estimates of the guns/butter effect in spending preferences, conditional on preferences for government-financed job creation
#######################################################################################
gvb = read.dta("Guns vs Butter.dta", convert.underscore=TRUE)
summary(gvb)
us = subset(gvb, ccode == 2)

test <- function(x) {x}

### Scatterplot
s <- ggplot(gvb, aes(x = d1, y = d0)) + geom_point()
s <- s + stat_function(fun = test, geom = "line")
s <- s + geom_hline(yintercept = 0, linetype = "dashed") + geom_vline(xintercept = 0, linetype = "dashed")
s <- s + geom_point(data = us, aes(x = d1, y = d0), shape = 1, size = 5, color = "red")
s <- s + xlab("Guns/Butter Effect: Create Jobs = 1") + ylab("Guns/Butter Effect: Create Jobs = 0")
s <- s + expand_limits(y=c(-0.5, 0.75)) + theme_bw()
s


